﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Grundy.Common;
using Grundy.Entity;
using Grundy.Message;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Grundy.Licensing.Server.Tests
{
    [TestClass]
    public class UncountedConcurrentSessionsStrategyTests
    {
        private License license;

        [TestInitialize]
        public void Setup()
        {
            license = new UncountedFloatingLicense{Client = LicenseClientType.Machine};
        }

        [TestMethod]
        public void Should_add_session_to_client_sessions()
        {
            var fik = new FeatureIdentificationKey();
            var strategy = new UncountedConcurrentSessionsStrategy(license, LicenseEntitlementValidity.Null, fik);
            var sessionId = strategy.Checkout(new LicenseRequestInformation { ClientIdentifiers = new IdentifierCollection(), ClientSessionId = Guid.NewGuid(), FeatureIdentificationKey = fik }, new object());

            Assert.AreEqual(1, strategy.GetClientSessions().Count());
            Assert.IsTrue(strategy.HasClientSession(sessionId));
        }

        [TestMethod]
        public void Should_not_add_session_to_client_sessions_if_client_session_already_exists()
        {
            var fik = new FeatureIdentificationKey();
            var strategy = new UncountedConcurrentSessionsStrategy(license, LicenseEntitlementValidity.Null, fik);
            var identifierCollection = new IdentifierCollection();
            var sessionId = strategy.Checkout(new LicenseRequestInformation { ClientIdentifiers = identifierCollection, ClientSessionId = Guid.NewGuid(), FeatureIdentificationKey = fik }, new object());
            var returnedSessionId = strategy.Checkout(new LicenseRequestInformation { ClientIdentifiers = identifierCollection, ClientSessionId = Guid.NewGuid(), FeatureIdentificationKey = fik }, new object());

            Assert.AreEqual(1, strategy.GetClientSessions().Count());
            Assert.AreEqual(sessionId, returnedSessionId);
        }
    }
}